<template>
	<view class="container">
		<view class="content">
			<view class="title">欢迎进入绿荫英雄~</view>
			<view class="subtitle">正在登陆，请稍等...</view>
			<view class="btn">
				<uv-button text="登录" color="#2a2a43" @click="onLogin"></uv-button>
				<!-- <view class="cancel" @click="close">暂不登录</view> -->
			</view>
		</view>
	</view>
</template>

<script setup>
import { openidLogin } from '@/api/auth';
import { useAuthStore } from '@/store';
import $tools from '@/utils/common';

const router = useRouter();
const userStore = useAuthStore();

const pageQuery = ref(undefined);

onLoad((query) => {
	console.log(query);
	pageQuery.value = query;
});
onShow(() => {});

const onLogin = () => {
	uni.login({
		provider: 'weixin', //使用微信登录
		success: function (res) {
			console.log(res);
			openidLogin({ code: res.code }).then((res) => {
				$tools.toast('登录成功', { duration: 1500 });
				const userinfo = res.data.userinfo;
				console.log(userinfo);
				userStore.handleLoginInfo(userinfo);
				setTimeout(() => {
					if (unref(pageQuery)?.redirect) {
						// 如果有存在redirect(重定向)参数，登录成功后直接跳转
						const params = omit(unref(pageQuery), ['redirect', 'tabBar']);
						if (unref(pageQuery)?.tabBar) {
							// 这里replace方法无法跳转tabbar页面故改为replaceAll
							router.replaceAll({ name: unref(pageQuery)?.redirect, params });
						} else {
							router.replace({ name: unref(pageQuery)?.redirect, params });
						}
					} else {
						// 不存在则返回上一页
						router.back();
					}
				}, 1500);
			});
		},
	});
};
</script>

<style lang="scss" scoped>
.content {
	padding: 50rpx;
	text-align: center;
	.title {
		margin-top: 200rpx;
		font-size: 32rpx;
		font-weight: 500;
		color: #0e0d0d;
	}
	.subtitle {
		margin-top: 40rpx;
		font-size: 26rpx;
		font-weight: 400;
		color: #666666;
	}
	.btn {
		width: 350rpx;
		margin: 0 auto;
		margin-top: 80rpx;
		.cancel {
			margin-top: 30rpx;
			font-size: 22rpx;
			color: #999999;
		}
	}
}
</style>
